<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>


          <a-col :span="8">
            <a-form-model-item label="姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder=""></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="性别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sex">
              <j-dict-select-tag type="list" v-model="model.sex" dictCode="sex" placeholder="" />
            </a-form-model-item>
          </a-col>

          <a-col :span="8">

            <a-form-model-item label="监室号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orgCode">
              <j-select-depart v-model="model.orgCode" multi />
            </a-form-model-item>

          </a-col>

          <a-col :span="8">
            <a-form-model-item label="证件号码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="idNum">
              <a-input v-model="model.idNum" placeholder=""></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="16">
            <a-form-model-item label="现住址详址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="address">
              <a-input v-model="model.address" placeholder=""></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="入所时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="custodyDate">
              <j-date placeholder="" v-model="model.custodyDate" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <!--          <a-col :span="8" >-->
          <!--            <a-form-model-item label="出所时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="releaseDate">-->
          <!--              <j-date placeholder="" v-model="model.releaseDate" style="width: 100%" />-->
          <!--            </a-form-model-item>-->
          <!--          </a-col>-->
          <a-col :span="8">
            <a-form-model-item label="人员状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag type="list" v-model="model.status = 1" dictCode="fpms_suspects_status" placeholder="在所人员"
                disabled trigger />
            </a-form-model-item>
          </a-col>


          <!--          <a-col :span="8" >-->
          <!--            <a-form-model-item label="本月订购" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderSign">-->
          <!--              <j-dict-select-tag type="list" v-model="model.orderSign" dictCode="order_sign" placeholder="请选择本月订购" />-->
          <!--            </a-form-model-item>-->
          <!--          </a-col>-->


          <a-col :span="8">
            <a-form-model-item label="账户余额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="balance">
              <a-input-number :disabled='$store.state.user.zhou' v-model="model.balance" placeholder=""
                style="width: 100%" />
            </a-form-model-item>

          </a-col>
          <!--          <a-col :span="8" >-->
          <!--            <a-form-model-item label="总充值金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rechargeAmount">-->
          <!--              <a-input-number v-model="model.rechargeAmount" placeholder="" style="width: 100%" />-->
          <!--            </a-form-model-item>-->
          <!--          </a-col>-->
          <!--          <a-col :span="8" >-->
          <!--            <a-form-model-item label="总消费金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="consumptionAmount">-->
          <!--              <a-input-number v-model="model.consumptionAmount" placeholder="" style="width: 100%" />-->
          <!--            </a-form-model-item>-->
          <!--          </a-col>-->
          <a-col :span="8">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remarks">
              <a-input v-model="model.remarks" placeholder="请输入备注"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
    <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="余额变动表" :key="refKeys[0]" :forceRender="true">
        <j-editable-table :ref="refKeys[0]" :loading="fpmsBalanceChangeTable.loading"
          :columns="fpmsBalanceChangeTable.columns" :dataSource="fpmsBalanceChangeTable.dataSource" :maxHeight="300"
          :disabled="formDisabled" :rowNumber="true" :rowSelection="true" :actionButton="true" />
      </a-tab-pane>
      <a-tab-pane tab="人员变动表" :key="refKeys[1]" :forceRender="true">
        <j-editable-table :ref="refKeys[1]" :loading="fpmsSuspectsChangeTable.loading"
          :columns="fpmsSuspectsChangeTable.columns" :dataSource="fpmsSuspectsChangeTable.dataSource" :maxHeight="300"
          :disabled="formDisabled" :rowNumber="true" :rowSelection="true" :actionButton="true" />
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>
import { mapMutations, mapState } from 'vuex'
import { getAction } from '@/api/manage'
import { FormTypes, getRefPromise, VALIDATE_NO_PASSED } from '@/utils/JEditableTableUtil'
import { JEditableTableModelMixin } from '@/mixins/JEditableTableModelMixin'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'FpmsSuspectsForm',
  mixins: [JEditableTableModelMixin],
  components: {
  },
  data () {
    return {
      zhou: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      labelCol2: {
        xs: { span: 24 },
        sm: { span: 3 },
      },
      wrapperCol2: {
        xs: { span: 24 },
        sm: { span: 20 },
      },
      model: {
      },
      // 新增时子表默认添加几行空数据
      addDefaultRowNum: 0,
      validatorRules: {
        name: [
          { required: true, message: '请输入姓名!' },
        ],
        // status:[{required: true, message: '请选择人员状态!'}],
        orgCode: [{ required: true, message: '请选择所在监室!' }],
        idNum: [
          { required: true, message: '请输入证件号码!' },
          { validator: (rule, value, callback) => validateDuplicateValue('fpms_suspects', 'id_num', value, this.model.id, callback) },
        ],
        balance: [
          { required: false, message: '请输入账户余额!' },
        ],
        // rechargeAmount: [
        //    { required: true, message: '请输入总充值金额!'},
        // ],
        // consumptionAmount: [
        //    { required: true, message: '请输入总消费金额!'},
        // ],
      },
      refKeys: ['fpmsBalanceChange', 'fpmsSuspectsChange',],
      tableKeys: ['fpmsBalanceChange', 'fpmsSuspectsChange',],
      activeKey: 'fpmsBalanceChange',
      // 余额变动表
      fpmsBalanceChangeTable: {
        loading: false,
        dataSource: [],
        columns: [
          {
            title: '变动说明',
            key: 'changes',
            type: FormTypes.select,
            dictCode: "balance_change_reason",
            width: "200px",
            placeholder: '',
            defaultValue: '',
          },
          {
            title: '订单号',
            key: 'changesOrder',
            type: FormTypes.input,
            width: "200px",
            placeholder: '',
            defaultValue: "0",
          },
          {
            title: '变动金额',
            key: 'amount',
            type: FormTypes.inputNumber,
            width: "120px",
            placeholder: '',
            defaultValue: 0,
          },
          {
            title: '变更前余额',
            key: 'previousBalance',
            type: FormTypes.inputNumber,
            width: "120px",
            placeholder: '',
            defaultValue: 0,
          },
          {
            title: '变更后余额',
            key: 'currentBalance',
            type: FormTypes.inputNumber,
            width: "120px",
            placeholder: '',
            defaultValue: '',
          },
          {
            title: '操作时间',
            key: 'operatingTime',
            type: FormTypes.date,
            disabled: true,
            width: "200px",
            placeholder: '',
            defaultValue: '',
          },
          {
            title: '操作账号',
            key: 'operator',
            type: FormTypes.input,
            disabled: true,
            width: "120px",
            placeholder: '',
            defaultValue: '',
          },
          // {
          //   title: '操作ip',
          //   key: 'operatingIp',
          //   type: FormTypes.input,
          //   disabled:true,
          //   width:"200px",
          //   placeholder: '请输入${title}',
          //   defaultValue:'',
          // },
          // {
          //   title: '备注',
          //   key: 'remarks',
          //   type: FormTypes.input,
          //   width:"200px",
          //   placeholder: '请输入${title}',
          //   defaultValue:'',
          // },
        ]
      },
      // 人员变动表
      fpmsSuspectsChangeTable: {
        loading: false,
        dataSource: [],
        columns: [
          {
            title: '变更事项',
            key: 'changes',
            type: FormTypes.select,
            dictCode: "suspects_change_reason",
            width: "200px",
            placeholder: '',
            defaultValue: '',
          },
          {
            title: '变更日期',
            key: 'changeDate',
            type: FormTypes.date,
            width: "200px",
            placeholder: '',
            defaultValue: '',
          },
          {
            title: '原监室',
            key: 'oldRoom',
            type: FormTypes.input,
            width: "200px",
            placeholder: '',
            defaultValue: '',
          },
          {
            title: '原状态',
            key: 'oldState',
            type: FormTypes.select,
            dictCode: "fpms_suspects_status",
            width: "200px",
            placeholder: '',
            defaultValue: '',
          },
          {
            title: '变更后监室',
            key: 'newRoom',
            type: FormTypes.input,
            width: "200px",
            placeholder: '',
            defaultValue: '',
          },
          {
            title: '变更后状态',
            key: 'newState',
            type: FormTypes.select,
            dictCode: "fpms_suspects_status",
            width: "200px",
            placeholder: ' ',
            defaultValue: '',
          },
        ]
      },
      url: {
        add: "/suspects/fpmsSuspects/add",
        edit: "/suspects/fpmsSuspects/edit",
        queryById: "/suspects/fpmsSuspects/queryById",
        fpmsBalanceChange: {
          list: '/suspects/fpmsSuspects/queryFpmsBalanceChangeByMainId'
        },
        fpmsSuspectsChange: {
          list: '/suspects/fpmsSuspects/queryFpmsSuspectsChangeByMainId'
        },
      }
    }
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  computed: {
    ...mapState('app', ['zhou']),
    formDisabled () {
      return this.disabled
    },
  },
  created () {
  },
  methods: {
    addBefore () {
      this.fpmsBalanceChangeTable.dataSource = []
      this.fpmsSuspectsChangeTable.dataSource = []
    },
    getAllTable () {
      let values = this.tableKeys.map(key => getRefPromise(this, key))
      return Promise.all(values)
    },
    /** 调用完edit()方法之后会自动调用此方法 */
    editAfter () {
      this.$nextTick(() => {
      })
      // 加载子表数据
      if (this.model.id) {
        let params = { id: this.model.id }
        this.requestSubTableData(this.url.fpmsBalanceChange.list, params, this.fpmsBalanceChangeTable)
        this.requestSubTableData(this.url.fpmsSuspectsChange.list, params, this.fpmsSuspectsChangeTable)
      }
    },
    //校验所有一对一子表表单
    validateSubForm (allValues) {
      return new Promise((resolve, reject) => {
        Promise.all([
        ]).then(() => {
          resolve(allValues)
        }).catch(e => {
          if (e.error === VALIDATE_NO_PASSED) {
            // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
            this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
          } else {
            console.error(e)
          }
        })
      })
    },
    /** 整理成formData */
    classifyIntoFormData (allValues) {
      let main = Object.assign(this.model, allValues.formValue)
      return {
        ...main, // 展开
        fpmsBalanceChangeList: allValues.tablesValue[0].values,
        fpmsSuspectsChangeList: allValues.tablesValue[1].values,
      }
    },
    validateError (msg) {
      this.$message.error(msg)
    },

  }
}
</script>

<style scoped></style>